﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.DataSource;
using EkStudio.com.common.unit;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.RootInterface.Field;

namespace EkStudio.com.ui.form.Auto
{
    public partial class AutoQueryExpForm : DevExpress.XtraEditors.XtraForm
    {

        private BillFormatModule module = null;

        private MemoEdit parentMemo = null;

        private List<SelectItem> selectList = null;

        private AutoQueryFormula MainForm = null;

        private bool fromUi = false;

        public AutoQueryExpForm(BillFormatModule self,AutoQueryFormula mainForm)
        {
            InitializeComponent();
            this.module = self;
            this.MainForm = mainForm;
        }

        

        public void RegeditDemo(List<SelectItem> billList,MemoEdit memo)
        {
            fromUi = false;
            selectList = billList;
            parentMemo = memo;
            //添加第一个字段值
            FirstBoxList.Properties.Items.Clear();
            SecondBoxList.Properties.Items.Clear();
            SecondBoxList.Properties.Items.Add(new ComboxItemMark("", "当前界面取值", ""));
            foreach (SelectItem it in selectList)
            {
                if (it.MainTable)
                {
                    ComboxItemMark mark = new ComboxItemMark("record_id", it.Caption + ".record_id", it.TableName);
                    FirstBoxList.Properties.Items.Add(mark);
                    SecondBoxList.Properties.Items.Add(mark);
                    mark = new ComboxItemMark("user_id", it.Caption + ".user_id", it.TableName);
                    FirstBoxList.Properties.Items.Add(mark);
                    SecondBoxList.Properties.Items.Add(mark);
                    mark = new ComboxItemMark("status", it.Caption + ".status", it.TableName);
                    FirstBoxList.Properties.Items.Add(mark);
                    SecondBoxList.Properties.Items.Add(mark);

                }
                else
                {
                    ComboxItemMark mark = new ComboxItemMark("record_id", it.Caption + ".record_id", it.TableName);
                    FirstBoxList.Properties.Items.Add(mark);
                    SecondBoxList.Properties.Items.Add(mark);
                    mark = new ComboxItemMark("parent_id", it.Caption + ".parent_id", it.TableName);
                    FirstBoxList.Properties.Items.Add(mark);
                    SecondBoxList.Properties.Items.Add(mark);
                    mark = new ComboxItemMark("status", it.Caption + ".status", it.TableName);
                    FirstBoxList.Properties.Items.Add(mark);
                    SecondBoxList.Properties.Items.Add(mark);
                }
                if (it.Value != null && it.Value.Fields != null && it.Value.Fields.Count > 0)
                {
                    foreach (BillSourceFields field in it.Value.Fields)
                    {
                        ComboxItemMark mark = new ComboxItemMark(field.Key, string.Format("{0}.{1}", it.Caption, field.Caption), it.TableName);
                        FirstBoxList.Properties.Items.Add(mark);
                        SecondBoxList.Properties.Items.Add(mark);
                    }
                }
            }
        }

        private void LoadSelect()
        {
            fromUi = false;
            SecondBoxList.EditValue = "";
            //添加第一个字段值
            SecondBoxList.Properties.Items.Clear();
            SecondBoxList.Properties.Items.Add(new ComboxItemMark("", "当前界面取值", ""));
            foreach (SelectItem it in selectList)
            {
                if (it.MainTable)
                {
                    ComboxItemMark mark = new ComboxItemMark("record_id", it.Caption + ".record_id", it.TableName);
                    
                    SecondBoxList.Properties.Items.Add(mark);
                    mark = new ComboxItemMark("user_id", it.Caption + ".user_id", it.TableName);
                    
                    SecondBoxList.Properties.Items.Add(mark);
                    mark = new ComboxItemMark("status", it.Caption + ".status", it.TableName);
                    
                    SecondBoxList.Properties.Items.Add(mark);

                }
                else
                {
                    ComboxItemMark mark = new ComboxItemMark("record_id", it.Caption + ".record_id", it.TableName);
                    
                    SecondBoxList.Properties.Items.Add(mark);
                    mark = new ComboxItemMark("parent_id", it.Caption + ".parent_id", it.TableName);
                    
                    SecondBoxList.Properties.Items.Add(mark);
                    mark = new ComboxItemMark("status", it.Caption + ".status", it.TableName);
                    
                    SecondBoxList.Properties.Items.Add(mark);
                }
                if (it.Value != null && it.Value.Fields != null && it.Value.Fields.Count > 0)
                {
                    foreach (BillSourceFields field in it.Value.Fields)
                    {
                        ComboxItemMark mark = new ComboxItemMark(field.Key, string.Format("{0}.{1}", it.Caption, field.Caption), it.TableName);
                        
                        SecondBoxList.Properties.Items.Add(mark);
                    }
                }
            }
        }



        #region 移动
        private bool isMouseDown = false;
        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置

        private void AutoQuerySelect_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
                FormLocation = this.Location;
                mouseOffset = Control.MousePosition;
            }
        }

        private void AutoQuerySelect_MouseMove(object sender, MouseEventArgs e)
        {
            int _x = 0;
            int _y = 0;
            if (isMouseDown)
            {
                Point pt = Control.MousePosition;
                _x = mouseOffset.X - pt.X;
                _y = mouseOffset.Y - pt.Y;

                this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
            }
        }

        private void AutoQuerySelect_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        #endregion

        private void EqualBoxList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        /// <summary>
        /// And
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (parentMemo != null)
            {
                parentMemo.Text = parentMemo.Text + " and ";

            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (parentMemo != null)
            {
                parentMemo.Text = parentMemo.Text + " or ";
            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (parentMemo != null)
            {
                parentMemo.Text = parentMemo.Text + " ( ";
            }
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (parentMemo != null)
            {
                parentMemo.Text = parentMemo.Text + " ) ";
            }
        }
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            string FirstParam = "";
            StringBuilder sql = new StringBuilder();
            if(FirstBoxList.EditValue is ComboxItemMark)
            {
                ComboxItemMark mark = FirstBoxList.EditValue as ComboxItemMark;
                FirstParam = mark.Key;
                sql.Append(mark.TableName).Append(".").Append(FirstParam);
            }
            else
            {
                FirstParam = FirstBoxList.EditValue.ToString();
                sql.Append(FirstParam);
            }

            sql.Append(" ").Append(EqualBoxList.EditValue.ToString()).Append(" ");

            if(SecondBoxList.EditValue is ComboxItemMark)
            {
                ComboxItemMark mark = SecondBoxList.EditValue as ComboxItemMark;
                if (fromUi)
                {
                    sql.Append(":").Append(FirstParam);
                    this.MainForm.AddParamGridRow(FirstParam, mark.Caption, mark.Key);
                }
                else
                {
                    sql.Append(mark.TableName).Append(".").Append(mark.Key);
                }
                
            }
            else
            {
                sql.Append(SecondBoxList.EditValue.ToString());
            }

            parentMemo.Text = string.Format("{0} {1}", parentMemo.Text, sql.ToString());

            FirstBoxList.EditValue = "";
            SecondBoxList.EditValue = "";
        }

        private void EqualBoxList_EditValueChanged(object sender, EventArgs e)
        {
            string value = EqualBoxList.EditValue.ToString();
            if(value == "is not null" || value == "is null")
            {
                SecondBoxList.EditValue = "";
                SecondBoxList.Enabled = false;
            }
            else
            {
                SecondBoxList.Enabled = true;
            }
        }



        private void BtnNewLine_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            parentMemo.Text = string.Format("{0} {1}", parentMemo.Text, Environment.NewLine);
        }

        private void SecondBoxList_EditValueChanged(object sender, EventArgs e)
        {
            if (SecondBoxList.EditValue.ToString() == "当前界面取值")
            {
                SecondBoxList.EditValue = "";
                fromUi = true;
                SecondBoxList.Properties.Items.Clear();
                SecondBoxList.Properties.Items.Add(new ComboxItemMark("", "取已选表结构", ""));
                if(module.getBillType() == ProjectUtil.TYPE_BILL_MODULE)
                {
                    if (module.DataSource != null && module.DataSource.TableItems != null && module.DataSource.TableItems.Count > 0)
                    {
                        foreach (BillSourceTabItems tab in module.DataSource.TableItems)
                        {
                            if (tab.Fields != null && tab.Fields.Count > 0)
                            {
                                foreach (BillSourceFields field in tab.Fields)
                                {
                                    ComboxItemMark mark = new ComboxItemMark(field.Key, string.Format("{0}.{1}", tab.Caption, field.Caption), tab.TableName);
                                    SecondBoxList.Properties.Items.Add(mark);
                                }
                            }
                        }
                    }
                }else if(module.getBillType() == ProjectUtil.TYPE_BILL_MULTIPLE)
                {
                    if (module.UiField != null && module.UiField.FieldsProperties != null && module.UiField.FieldsProperties.Count > 0)
                    {
                        foreach(BillUiGroupItems group in module.UiField.FieldsProperties)
                        {
                            if (group.Fields!=null)
                            {
                                foreach(BillUiFieldsItem field in group.Fields)
                                {
                                    ComboxItemMark mark = new ComboxItemMark(field.Key, string.Format("{0}.{1}", group.Caption, field.Caption),"");
                                    SecondBoxList.Properties.Items.Add(mark);
                                }
                            }
                        }
                    }
                }
                
            }
            else if(SecondBoxList.EditValue.ToString() == "取已选表结构")
            {
                LoadSelect();
            }
        }

        private class ComboxItemMark
        {
            private string key = "";
            private string caption = "";
            private string tableName = "";

            public ComboxItemMark(string field,string caption,string tablename)
            {
                this.key = field;
                this.caption = caption;
                this.tableName = tablename;
            }

            public string Key
            {
                get
                {
                    return key;
                }

                set
                {
                    key = value;
                }
            }

            public string Caption
            {
                get
                {
                    return caption;
                }

                set
                {
                    caption = value;
                }
            }

            public string TableName
            {
                get
                {
                    return tableName;
                }

                set
                {
                    tableName = value;
                }
            }

            public override string ToString()
            {
                return caption;
            }

        }

    }












}